package com.macbulldev.poc.testgin.controller;

import java.util.List;


import org.apache.log4j.Logger;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.macbulldev.poc.testgin.domain.EventLog;


@org.springframework.roo.addon.web.mvc.controller.RooWebScaffold(path = "eventlog", automaticallyMaintainView = false, formBackingObject = com.macbulldev.poc.testgin.domain.EventLog.class)
@org.springframework.web.bind.annotation.RequestMapping("/eventlog/**")
@org.springframework.stereotype.Controller
public class EventLogController 
{
	
	private static final Logger LOGGER = Logger.getLogger(EventLogController.class);


	@RequestMapping(value = "/eventlog/{id}", method = RequestMethod.GET)    
    public String show(@PathVariable("id") Long id, ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("eventLog_createTs_date_format", org.joda.time.format.DateTimeFormat.patternForStyle("S-", org.springframework.context.i18n.LocaleContextHolder.getLocale()));        
        modelMap.addAttribute("eventLog", EventLog.findEventLog(id));        
        return "eventlog/show";        
    }

	@RequestMapping(value = "/eventlog", method = RequestMethod.GET)    
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, ModelMap modelMap) {    
        if (page != null || size != null) {        
            int sizeNo = size == null ? 10 : size.intValue();            
            modelMap.addAttribute("eventlogs", EventLog.findEventLogEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));            
            float nrOfPages = (float) EventLog.countEventLogs() / sizeNo;            
            modelMap.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));            
        } else {        
            modelMap.addAttribute("eventlogs", EventLog.findAllEventLogs());            
        }        
        modelMap.addAttribute("eventLog_createTs_date_format", org.joda.time.format.DateTimeFormat.patternForStyle("S-", org.springframework.context.i18n.LocaleContextHolder.getLocale()));        
        return "eventlog/list";        
    }
	
	@RequestMapping(value = {"/ajax/eventlog/searchform","/eventlog/searchform"},method=RequestMethod.GET)
	public String renderEvnetLogSearchForm(Model model)
	{
		 return "eventlog/eventlog_search";
	}
	
	/*
	@RequestMapping(value = {"/ajax/eventlog/search","/eventlog/search"},method=RequestMethod.GET)
	public String processEvnetLogSearchForm(Model model,EventLog eventLog)
	{
		
		 List<EventLog> eventLogs = eventLog.findByExample(eventLog,0, 20);
		 model.addAttribute("eventlogs",eventLogs);
		 
		
		 return "eventlog/eventlog_search_results";
	}
	*/
	
	@RequestMapping(value = "/eventlog/search", method = RequestMethod.GET)    
    public String processEventLogSearchForm(@RequestParam(value = "pageNumber", required = false) Integer page, EventLog eventLog, ModelMap modelMap) {    
            int sizeNo = 20;           
            int pageNumber= page == null ? 0 : page;
           
            modelMap.addAttribute("eventlogs", eventLog.findByExample(pageNumber*sizeNo, sizeNo));            
            Long count = eventLog.countByExample();
            modelMap.addAttribute("maxPageNumber", (Long)(count/sizeNo));
        modelMap.addAttribute("eventLog_createTs_date_format", org.joda.time.format.DateTimeFormat.patternForStyle("S-", org.springframework.context.i18n.LocaleContextHolder.getLocale()));        
        return "eventlog/eventlog_search_results";        
    }
	
	@RequestMapping(value = "/eventlog/list", method = RequestMethod.GET)    
    public String renderEventLogList(@RequestParam(value = "pageNumber", required = false) Integer page, ModelMap modelMap) {    

		
		int sizeNo = 20;           
        int pageNumber= page == null ? 0 : page;
           
        modelMap.addAttribute("eventlogs",EventLog.findEventLogEntries(pageNumber*sizeNo, sizeNo));            
        Long count = EventLog.countEventLogs();
        modelMap.addAttribute("maxPageNumber", (Long)(count/sizeNo));
        return "eventlog/list";        
    }
	

//	@RequestMapping(method = RequestMethod.PUT)    
//    public String update(@Valid EventLog eventLog, BindingResult result, ModelMap modelMap) {    
//        if (eventLog == null) throw new IllegalArgumentException("A eventLog is required");        
//        if (result.hasErrors()) {        
//            modelMap.addAttribute("eventLog", eventLog);            
//            modelMap.addAttribute("eventLog_createTs_date_format", org.joda.time.format.DateTimeFormat.patternForStyle("S-", org.springframework.context.i18n.LocaleContextHolder.getLocale()));            
//            return "eventlog/update";            
//        }        
//        eventLog.merge();        
//        return "redirect:/eventlog/" + eventLog.getId();        
//    }

//	@RequestMapping(value = "/eventlog/{id}/form", method = RequestMethod.GET)    
//    public String updateForm(@PathVariable("id") Long id, ModelMap modelMap) {    
//        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
//        modelMap.addAttribute("eventLog", EventLog.findEventLog(id));        
//        modelMap.addAttribute("eventLog_createTs_date_format", org.joda.time.format.DateTimeFormat.patternForStyle("S-", org.springframework.context.i18n.LocaleContextHolder.getLocale()));        
//        return "eventlog/update";        
//    }

	
}
